package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.wx.vo.UserRegCaptchaBo;
import com.cskaoyan.bean.wx.vo.UserRegisterBo;
import com.cskaoyan.bean.wx.vo.UserResetBo;
import com.cskaoyan.bean.wx.vo.WxUserLoginVO;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.MailService;
import com.cskaoyan.service.UserService;
import com.cskaoyan.shiro.MallToken;
import com.cskaoyan.utils.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;


//@RestController
//@RequestMapping("wx/auth")
public class WechatAuthController {
    @Autowired
    UserService userService;
    @Autowired
    MailService mailService;


    @PostMapping("register")
    public BaseRespVo register(HttpServletRequest request, @RequestBody UserRegisterBo userRegisterBo) {

        String code = (String) request.getServletContext().getAttribute("code");
        if (code == null) return BaseRespVo.fail(703, "验证码过期");
        /*一个用户一台手机*/
        if (!userRegisterBo.getCode().equals(code)) return BaseRespVo.fail(703, "验证码错误");
        User user = new User();
        String username = userRegisterBo.getUsername();
        user.setUsername(username);
        user.setNickname(username);
        String md5Pwd = EncryptUtil.MD5(userRegisterBo.getPassword());
        user.setPassword(md5Pwd);
        user.setMobile(userRegisterBo.getMobile());
        //获得ip地址
        String ip = request.getRemoteAddr();
        ip= Constant.MY_IP.equals(ip) ?Constant.IP : ip;
        user.setLastLoginIp(ip);
        user.setWeixinOpenid(userRegisterBo.getWxCode());
        int rows = userService.insert(user);
        if (rows ==  Constant.CHANGE_FAIL) return BaseRespVo.fail(704, "用户名或邮箱已注册");

        //注册成功制作登录凭证
        MallToken mallToken = new MallToken(username, md5Pwd, "WxUser");
        Subject subject = SecurityUtils.getSubject();

        //登陆 失败会有异常
        subject.login(mallToken);

        //获取用户信息
        User u = (User) subject.getPrincipal();
        Serializable id = subject.getSession().getId();

        //制作返回的用户信息
        WxUserLoginVO wxUserLoginVO = new WxUserLoginVO();
        wxUserLoginVO.setToken(id.toString());
        WxUserLoginVO.UserInfo userInfo = wxUserLoginVO.getUserInfo();
        userInfo.setAvatarUrl(u.getAvatar());
        userInfo.setNickName(u.getNickname());
        return BaseRespVo.ok(u);
    }

    @PostMapping("reset")
    public BaseRespVo reset(HttpServletRequest request, @RequestBody UserResetBo userRegisterBo) {
        String code = (String)request.getServletContext().getAttribute("code");
        if (!userRegisterBo.getCode().equals(code)) return BaseRespVo.fail(703, "验证码错误");
        User user = new User();
        user.setPassword(EncryptUtil.MD5(userRegisterBo.getPassword()));
        user.setMobile(userRegisterBo.getMobile());
        user.setUpdateTime(CommonUtil.nowTime());
        String ip = request.getRemoteAddr();
        ip= Constant.MY_IP.equals(ip) ?Constant.IP : ip;
        user.setLastLoginIp(ip);
        int rows = userService.updateInWechat(user);
        if (rows == Constant.CHANGE_FAIL) return BaseRespVo.fail(703, "更新失败");
        else return BaseRespVo.fail(703, "更新成功，请点击重新进入");
    }

    @PostMapping("regCaptcha")
    public BaseRespVo regCaptcha(HttpServletRequest request, @RequestBody UserRegCaptchaBo userRegCaptchaBo) {
        String mobile = userRegCaptchaBo.getMobile();
        if (!mobile.matches(Constant.MAIL)) return BaseRespVo.fail(402, "邮箱格式不对");
        String code = RandomUtil.GetRandomString(Constant.LENGTH);
        String title = "前台验证码";
        String text = "您的验证码为" + code;

        boolean b = mailService.send_mail(mobile, title, text);
        if (!b) return BaseRespVo.fail(402, "邮箱验证码发送失败");

        ServletContext servletContext = request.getServletContext();
        servletContext.setAttribute("code", code);
        return BaseRespVo.ok();
    }
}
